<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
       
        // 获取 一个元素 在 数组 中 第一次出现的位置 如果没有出现(都不相等)则返回-1
        var str = "bbbbbb";
        var arr = ["aaa","aaa","bbb","ccc","bbb","ccc"];
        Array.prototype.indexOf3 = function(str){
            for(var i = 0;i < this.length;i++){
                // if(str == this[i]){
                //     return i;
                // }
                if(str == this[i]) return i;
            }
            // console.log("===");
            return -1;
        }

        var b = arr.indexOf3("bbb");
        console.log(b);


        function indexOf2(array,str,b = 0){
            // b = b ? b : 0;
            for(var i = b;i < array.length;i++){
                if(str == array[i]) return i;
            }
            // console.log("===");
            return -1;
        }
        var c = indexOf2(arr,"bbb");
        console.log(c);

        var cont = 0;
        for(var i = 0;i < arr.length;i++){
            if(str == arr[i]){
                cont++;
                console.log(i); //2
                break;
            }
        }
        if(cont == 0){
            console.log(-1);
        }


        // 判断str与 数组中每一个值 从前往后
        if(arr[0] == str){
            console.log(0);
        }else if(arr[1] == str){
            console.log(1);
        }else if(arr[2] == str){
            console.log(2);
        }else if(arr[3] == str){
            console.log(3);
        }else if(arr[4] == str){
            console.log(4);
        }else if(arr[5] == str){
            console.log(5);
        }else{
            console.log(-1);
        }
        var a = arr[0] == str ? "0" : arr[1] == str ? 1 : arr[2] == str ? 2 : arr[3] == str ? 3 : arr[4] == str ? 4 : arr[5] == str ? 5 : -1;
        console.log(a);       


    </script>
</body>

</html>